#include "socket.hpp"
#include "calculator.hpp"

#include <ctime>

static const size_t default_buffer_size = 128;

const int sr_times = 1; 

void Usage(const string &arg) {
    lg(WARNING, "\tUsage:./client <server_ip> <server_port>");
} 
int main(int argc, char *argv[]) {
    if(argc != 3) {
        Usage(argv[0]);
        return 1;
    }   
    srand(time(nullptr));

    string server_ip = argv[1];
    uint16_t port = stoi(argv[2]);
    Socket client;
    // 先建立连接
    client.socket();
    client.connect(port, server_ip);
    string packet;
    while(true) {
        static string ops = "+-*/%|^!";
        request req(rand() % 10, rand() % 10, ops[rand() % ops.size()]);
        req.print();
        // 序列化
        string s;
        req.serializer(&s);
        lg(INFO, "client serializer Request: %s", s.c_str());
        // 封装
        if(!encapsulate(&s)) {
            continue;
        }
        lg(INFO, "client encapsulate Request: %s", s.c_str());
        // 发送
        for(int i = 0; i < sr_times; ++i) {
            ssize_t n = write(client.get_sockfd(), s.c_str(), s.size());
            if(n < 0) {
                lg(ERROR, "write error: %s", strerror(errno));
                continue;
            }
            lg(INFO, "client write success");
        }
        
        // 接收服务器的响应
        char buffer[default_buffer_size];
        ssize_t n = read(client.get_sockfd(), buffer, sizeof(buffer) - 1);
        if(n <= 0) {
            if(n < 0) {
                lg(FATAL, "read error: %s", strerror(errno));
            }
            else {
                lg(WARNING, "read end of file");
            }
            continue;
        }
        lg(INFO, "client read success");
        buffer[n] = '\0';
        packet += buffer;
        while(true) {
            // 解包
            string t;
            if(!decapsulate(&t, packet)) {
                break;
            }
            lg(INFO, "client decapsulate Response: %s", t.c_str());
            // 反序列化
            response res;
            res.deserializer(t);
            lg(INFO, "client deserializer Response: %s", res.get_result().c_str());
            if(res._status == right_ans) {
                lg(INFO, "Right answer: %d", res._result);
            }
            else {
                lg(WARNING, "Wrong answer: %s", res.get_result().c_str());
            }
            cout << endl;
        }
    }
    return 0;
}

// int main() {
//     request req(111, 111, '+');
//     string s;
//     req.serializer(&s);
//     cout << s << endl;
//     if(encapsulate(&s)) {
//         cout << s << endl;
//     }
//     string date;
//     if(decapsulate(&date, s)) {
//         cout << date << endl;
//     }
//     response res = calculator()(req);
//     cout << res.get_result() << endl;
//     cout << res._result << endl;
//     cout << "----------------------------------------------------------------" << endl;
//     res.serializer(&s);
//     cout << s << endl;
//     if(encapsulate(&s)) {
//         cout << s << endl;
//     }
//     if(decapsulate(&date, s)) {
//         cout << date << endl;
//     }
//     return 0;
// }

